/******************************************************************************/
/* Files to Include                                                           */
/******************************************************************************/

#if defined(__XC)
    #include <xc.h>         /* XC8 General Include File */
#elif defined(HI_TECH_C)
    #include <htc.h>        /* HiTech General Include File */
#endif

#include <stdint.h>         /* For uint8_t definition */
#include <stdbool.h>        /* For true/false definition */

#include "user.h"

/******************************************************************************/
/* User Functions                                                             */
/******************************************************************************/

/* <Initialize variables in user.h and insert code for user algorithms.> */

void ConfigureTMR1();

void InitApp(void)
{
   // PIC16F876
   // ?????????????? ????????????? ????????? ???????????
   STATUS = 0b00011000;  // 0b00011xxx - ????????? ???????????
   OPTION_REG = 0b01010111;  // 0b11111111 - RB0/INT, TMR0, WDT
   INTCON = 0b00000000;  // 0b0000000x - ??????????
   PIE1   = 0b00000000;  // 0b00000000 - ???????????? ?????????? (??????????)
   PIR1   = 0b00000000;  // 0b00000000 - ???????????? ?????????? (??????)
   PIE2   = 0b00000000;  // 0b00000000 - ???????????? ?????????? (??????????)
   PIR2   = 0b00000000;  // 0b00000000 - ???????????? ?????????? (??????)
   PCON   = 0b00000011;  // 0b00000001 - ???????? ?????? ???????????
   //PCL    = 0x00;        // 0x00       - ????? ??????????? ?????????? (???????)
   //PCLATH = 0x00;        // 0x00       - ????? ??????????? ?????????? (???????)
   //FSR    = 0x00         // 0xXX       - ????????? ?????????
   // 3.0 I/O PORTS

   TRISA  = 0b00000000;  // 0b11111111 - ??????????? ?????? 1-Input 0-Output
   TRISB  = 0b00000000;  // 0b11111111 - ??????????? ?????? 1-Input 0-Output
   TRISC  = 0b00000000;  // 0b11111111 - ??????????? ?????? 1-Input 0-Output
   TRISD  = 0b00000000;  // 0b11111111 - ??????????? ?????? 1-Input 0-Output
   TRISE  = 0b00000000;  // 0b11111111 - ??????????? ?????? 1-Input 0-Output

   PORTA  = 0b00000000;  // 0bxxxxxxxx
   PORTB  = 0b00000000;  // 0bxxxxxxxx
   PORTC  = 0b00000000;  // 0bxxxxxxxx
   PORTD  = 0b00000000;
   PORTE  = 0b00000000;

   // 4.0 EEPROM
   EEADR  = 0x00;        // 0xXX
   EEADRH = 0x00;        // 0xXX
   EEDATA = 0x00;        // 0xXX
   EEDATH = 0x00;        // 0xXX
   EECON1 = 0b00000000;  // 0bx000x000 - ????????? EEPROM
   // 5.0 TIMER0 MODULE
   TMR0   = 0x00;        // 0xXX
   // 6.0 TIMER1 MODULE
   TMR1H  = 0x00;        // 0xXX
   TMR1L  = 0x00;        // 0xXX
   T1CON  = 0b00000000;  // 0b00000000 - ????????? TMR1
   // 7.0 TIMER2 MODULE
   TMR2   = 0x00;        // 0x00
   PR2    = 0xFF;        // 0xFF
   T2CON  = 0b00000000;  // 0b00000000 - ????????? TMR2
   // 8.0 CCP  (CAPTURE/COMPARE/PWM MODULES)
   CCPR1H = 0x00;        // 0xXX
   CCPR1L = 0x00;        // 0xXX
   CCPR2H = 0x00;        // 0xXX
   CCPR2L = 0x00;        // 0xXX
   CCP1CON= 0b00000000;  // 0b00000000 - ????????? CCP1
   CCP2CON= 0b00000000;  // 0b00000000 - ????????? CCP2
   // 9.0 MSSP  (MASTER SYNCHRONOUS SERIAL PORT MODULE)
   SSPSTAT= 0b00000000;  // 0b00000000 - ????????? MSSP
   SSPCON = 0b00000000;  // 0b00000000 - ????????? MSSP
   SSPCON2= 0b00000000;  // 0b00000000 - ????????? MSSP
   SSPADD = 0x00;        // 0x00
   SSPBUF = 0x00;        // 0xXX
   // 10.0 USART  (ADDRESSABLE UNIVERSAL SYNCHRONOUS ASYNCHRONOUS RECEIVER TRANSMITTER)
   TXSTA  = 0b00000010;  // 0b00000010 - ????????? ???????????
   RCSTA  = 0b00000000;  // 0b0000000x - ????????? ?????????
   SPBRG  = 0x00;        // 0x00       - ????????? ????????
   //TXREG  = 0x00;        // 0x00       - ?????? ?? ????????
   //RCREG  = 0x00;        // 0x00       - ???????? ??????
    // 11.0 ADC  (ANALOG-TO-DIGITAL CONVERTER (A/D) MODULE)
   ADCON0 = 0b00000000;  // 0b00000000 - ????????? ADC
   ADCON1 = 0b00000000;  // 0b00000000 - ????????? ADC
   ADRESH = 0x00;        // 0xXX
   ADRESL = 0x00;        // 0xXX

   ConfigureTMR1();
}

void ConfigureTMR1()
{
    T1CON = 0b00000001; //Use OSC/4 as input with 1:1 prescaller
    CCP1CON = 0b00001010; 
    CCPR1L = 0x88;
    CCPR1H = 0x13; 
    CCP1IE = 1;

    PEIE =1;
    GIE =1;

};
